package com.ecarx.camera.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import com.ecarx.camera.R;
import com.ecarx.camera.ui.BaseDialog;
import com.ecarx.camera.ui.dialog.CustomDialog;
import com.jeremyliao.liveeventbus.utils.AppUtils;

public class DialogUtils {

    private static final String TAG = "DialogUtils";

    private DialogUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static void showPrivatePolicyDialog(@NonNull FragmentManager fragmentManager,
                                               @Nullable BaseDialog.OnClickListener onPositiveBtnClickListener,
                                               @Nullable BaseDialog.OnDismissListener onDismissListener) {
        LogUtils.d(TAG, "showPrivatePolicyDialog, fragmentManager = [" + fragmentManager + "], onPositiveBtnClickListener = [" + onPositiveBtnClickListener + "]");
        CharSequence title = Utils.getApp().getString(R.string.camera_custom_dialog_private_policy_title);
        CharSequence positiveTxt = Utils.getApp().getString(R.string.dialog_ok);
        int contentLayTopMargin = AppUtils.getApp().getResources().getDimensionPixelSize(R.dimen.camera_base_dialog_view_content_margin_top);
        if (onPositiveBtnClickListener == null) {
            contentLayTopMargin += AppUtils.getApp().getResources().getDimensionPixelSize(R.dimen.camera_main_view_title_bottom_margin_top);
        }
        BaseDialog baseDialog = CustomDialog.newInstance()
                .setTitle(title)
                .setContentLayTopMargin(contentLayTopMargin)
                .setPositiveBtn(positiveTxt, onPositiveBtnClickListener)
                .setCanceled(onPositiveBtnClickListener == null)
                .setCanceledOnTouchOutside(onPositiveBtnClickListener == null)
                .setOnDismissListener(onDismissListener)
                .setOnConfigurationChangeListener(dialog -> {
                    TextView tvMessage = dialog.layoutRoot.findViewById(R.id.tv_message);
                    tvMessage.setTextColor(ContextCompat.getColor(Utils.getApp(), R.color.camera_base_day_night_fg_color));
                });
        CustomDialog customDialog = ((CustomDialog) baseDialog);
        TextView customView = (TextView) LayoutInflater.from(Utils.getApp()).inflate(R.layout.layout_dialog_private_policy_tmp, null);
        customView.setMovementMethod(ScrollingMovementMethod.getInstance());
        customView.setMaxHeight(customView.getResources().getDimensionPixelSize(R.dimen.privacy_dialog_content_max_h));
        customDialog.setCustomView(customView);
        customDialog.show(fragmentManager);
    }

    /**
     * 显示下电的对话框
     * @param context
     */
    public static AlertDialog showAccOffDialog(Context context) {
        LogUtils.d(TAG, "showAccOffDialog");
        return showSingleBtnAlertDialog(context, R.string.dialog_title_dvr_error, R.string.dialog_desc_video_recording_unavailable, R.string.dialog_btn_dismiss, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(TAG, "showAccOffDialog, dismiss bnt click");
                if (context instanceof Activity) {
                    ((Activity)context).finish();
                }
            }
        }, false);
    }

    /**
     * 显示TfCard格式化的对话框
     * @param context
     * @param cancelClickListener
     * @param confirmClickListener
     * @return
     */
    public static Dialog showTfCardFormatDialog(Context context, View.OnClickListener cancelClickListener, View.OnClickListener confirmClickListener) {
        LogUtils.d(TAG, "showTfCardFormatDialog");
        return showAlertDialog(context, R.string.camera_settings_tf_card_format, R.string.dialog_tf_card_format_desc, R.string.dialog_btn_cancel, cancelClickListener, R.string.dialog_btn_format, confirmClickListener, false);
    }

    /**
     * 显示只有一个按钮的AlertDialog
     * @param context
     * @param titleStrResId
     * @param desStrResId
     * @param singleClickListener
     */
    public static AlertDialog showSingleBtnAlertDialog(Context context, int titleStrResId, int desStrResId, int btnTxtStrResId, View.OnClickListener singleClickListener, boolean cancelable) {
        if (context == null) {
            LogUtils.d(TAG, "showSingleBtnAlertDialog, but context is null");
            return null;
        }
        View lay = LayoutInflater.from(context).inflate(R.layout.alert_dialog_with_single_btn, null);
        ((TextView)lay.findViewById(R.id.dialog_title)).setText(titleStrResId);
        ((TextView)lay.findViewById(R.id.dialog_desc)).setText(desStrResId);
        ((TextView)lay.findViewById(R.id.dialog_single_btn)).setText(btnTxtStrResId);
        if (singleClickListener != null) {
            lay.findViewById(R.id.dialog_single_btn).setOnClickListener(singleClickListener);
        }
        AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.EAlertDialog)
                .setView(lay)
                .setCancelable(cancelable)
                .show();
        if (alertDialog != null) {
            alertDialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_width)
                            , context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_height));
        }
        return alertDialog;
    }

    /**
     * 显示带确认和取消按钮的AlertDialog
     * @param context
     * @param titleStrResId
     * @param desStrResId
     * @param btnCancelStrResId
     * @param cancelClickListener
     * @param btnConfirmStrResId
     * @param confirmClickListener
     * @param cancelable
     * @return
     */
    public static Dialog showAlertDialog(Context context, int titleStrResId, int desStrResId, int btnCancelStrResId, View.OnClickListener cancelClickListener, int btnConfirmStrResId, View.OnClickListener confirmClickListener, boolean cancelable) {
        if (context == null) {
            LogUtils.d(TAG, "showAlertDialog, but context is null");
            return null;
        }
        View lay = LayoutInflater.from(context).inflate(R.layout.alert_dialog_with_btns, null);
        Dialog alertDialog = new Dialog(context, R.style.EAlertDialog);
        alertDialog.setContentView(lay);
        alertDialog.setCancelable(cancelable);
        Window window = alertDialog.getWindow();
        if (window != null) {
            window.setLayout(context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_width)
                    , context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_height));
        }

        ((TextView)lay.findViewById(R.id.dialog_title)).setText(titleStrResId);
        ((TextView)lay.findViewById(R.id.dialog_desc)).setText(desStrResId);
        ((TextView)lay.findViewById(R.id.dialog_cancel_btn)).setText(btnCancelStrResId);
        if (cancelClickListener != null) {
            lay.findViewById(R.id.dialog_cancel_btn).setOnClickListener(cancelClickListener);
        }
        ((TextView)lay.findViewById(R.id.dialog_confirm_btn)).setText(btnConfirmStrResId);
        if (cancelClickListener != null) {
            lay.findViewById(R.id.dialog_confirm_btn).setOnClickListener(confirmClickListener);
        }

        alertDialog.show();

        return alertDialog;
    }

    /**
     * 显示加载中的AlertDialog
     * @param context
     * @param loadingStrResId
     * @param cancelable
     */
    public static AlertDialog showLoadingAlertDialog(Context context, int loadingStrResId, boolean cancelable) {
        if (context == null) {
            LogUtils.d(TAG, "showLoadingAlertDialog, but context is null");
            return null;
        }
        View lay = LayoutInflater.from(context).inflate(R.layout.alert_dialog_with_loading, null);
        ((TextView)lay.findViewById(R.id.dialog_loading_txt)).setText(loadingStrResId);
        AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.EAlertDialog)
                .setView(lay)
                .setCancelable(cancelable)
                .show();
        if (alertDialog != null) {
            alertDialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_loading_width)
                    , context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_loading_height));
        }
        return alertDialog;
    }
}
